/**
 * Copyright (C), 2018 store
 * Encoding: UTF-8
 * Date: 19-12-10 下午4:33
 * History:
 */
package com.swak.frame.excel.csv;

import java.io.FileNotFoundException;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.Resource;

import com.swak.frame.excel.AbstractLoaderSupportFactory;
import com.swak.frame.excel.bo.RowObject;
import com.swak.frame.excel.bo.SheetObject;
import com.swak.frame.excel.exception.ExcelException;


/**
 * CvsLoaderFactory.java
 * 
 * @author ColleyMa
 * @version 19-12-10 下午4:33
 */
public class CsvLoaderFactory extends AbstractLoaderSupportFactory {
    /** Logger available to subclasses */
    private CsvHandlerFactory handlerFactory;

    /** String colKey */
    private String colKey;

    public void setColKey(String colKey) {
        this.colKey = colKey;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.handlerFactory = new CsvHandlerFactory();
        super.afterPropertiesSet();
    }

    @Override
    public void loadProp(Map<String, Map<String, String>> localProp) {
        if (this.locations != null) {
            for (Resource location : this.locations) {
                long startTime = System.currentTimeMillis();
                logger.warn("loadding  cvs config file start,path:{} >>>>>>>>",
                    location.getFilename());
                try {
                    SheetObject sheetObject =
                        handlerFactory.getInputValuesByCvs(location, fileEncoding);
                    List<RowObject> rowList = sheetObject.getSheetData();

                    for (int i = 0; i < rowList.size(); i++) {
                        Map<String, String> rowMap = rowList.get(i).getCellValuesMap();
                        if (StringUtils.isEmpty(colKey)) {
                            colKey = sheetObject.getHeader().get(String.valueOf(0));
                        }
                        String keyVal = rowMap.get(colKey);
                        localProp.put(keyVal, rowMap);
                    }

                    logger.warn("loadding cvs config file end,path:{},cost:{} ms >>>>>>>>",
                        location.getFilename(), (System.currentTimeMillis() - startTime));
                } catch (Exception ex) {
                    // Resource not found when trying to open it
                    if ((ex instanceof FileNotFoundException
                        || ex instanceof UnknownHostException)) {
                        logger.warn("cvs resource not found: " + ex.getMessage());
                    } else {
                        throw new ExcelException("loadding cvs config file err", ex);
                    }
                }
            }
        }
    }

    @Override
    public Map<String, Map<String, String>> getLocalProp() {
        return localProp;
    }
}
